﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class UpdateCustomer : Form
    {
        private int id;
        public UpdateCustomer()
        {
            InitializeComponent();
        }
        public UpdateCustomer(int id)
        {
            InitializeComponent();
            this.id = id;
            onload(id);
        }

        private void onload(int id)
        {
            PurchaseDao dao = new PurchaseDao();
            DataRow row = dao.getCustomer(id);
            if (row != null)
            {
                txtName.Text = row["NAME"].ToString();
                rtbNote.Text = row["NOTE"].ToString();
                loadImageCapture(row["PICTURE"].ToString());
            }
        }
        public void loadImageCapture(string filename)
        {
            string path = filename;
            if (!String.IsNullOrEmpty(path))
            {
                try
                {
                    ptbImage.LoadAsync(path);
                    ptbImage.SizeMode = PictureBoxSizeMode.Zoom;
                    ptbImage.Text = filename;
                }
                catch (Exception ex)
                {
                    ptbImage.Image = null;
                    Console.WriteLine(ex);
                }
            }
        }
        private void btnCapture_Click(object sender, EventArgs e)
        {
            Capture capture = new Capture();
            capture.ShowDialog();
            loadImageCapture(capture.Filename);
        }
        private string getImageFolder()
        {
            return SqlHelper.readConfigFile(SqlHelper.IMAGE_FOLDER);
        }

        private void saveImage(string filename)
        {
            Image imge = ptbImage.Image;
            if (imge != null)
            {
                imge.Save(filename);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            string notes = rtbNote.Text.Trim();
            string path = getImageFolder();
            PurchaseDao dao = new PurchaseDao();
            if (name == "")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin");
            }
            else
            {
                String ide = dao.updateCustomer(id, name, notes, (path == null ? "" : path));

                if (ide != null && !"0".Equals(ide))
                {
                    saveImage(path + ide + ".JPG");
                    MessageBox.Show("Lưu thành công");
                    txtName.Text = "";
                    rtbNote.Text = "";
                    ptbImage.Image = null;
                    this.Dispose();
                }
                else
                {
                    MessageBox.Show("Lưu thất bại");
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            rtbNote.Text = "";
            ptbImage.Image = null;
        }

        private void ptbImage_Click(object sender, EventArgs e)
        {

        }

        private void ptbImage_DoubleClick(object sender, EventArgs e)
        {
            Capture capture = new Capture();
            capture.ShowDialog();
            loadImageCapture(capture.Filename);
        }
    }
}
